/* create-note.js */
function update_chars_left(max_len, target_input, display_element) {
		return false;
	  var lines = target_input.val().split('\n');
	  var enterCharacters = lines.length - 1;
	  
	  var text_len = target_input.val().length + enterCharacters;
	   if (text_len >= max_len) {
	       target_input.val(target_input.val().substring(0, max_len - enterCharacters)); // truncate
	       display_element.html('0 remaining');
	   } else {
	       display_element.html(max_len - text_len + " remaining");
	   }
}

jQuery(document).ready(function() {
	AJS.$('#note-create-dialog').live('submit', function(){
		// validate fields
		if (AJS.$(this).find('#update-content').val() == '') {
			alert('The content must be not empty!');
			return false;
		}
		// set redirect url
		AJS.$(this).find('[name="url"]').val(AJS.$(location).attr('href'));
 	});
	AJS.$('#update-content').live('keyup keydown',function(e) {
   		update_chars_left(255, jQuery(this), jQuery(this).parent().find("#update_remain"));
 	});

	// create a dialog 860px wide x 530px high
	var dlg_create = new AJS.Dialog({width:600, height:330, id:"note-create-dialog", closeOnOutsideClick: false});

	// adds header for second page
	dlg_create.addHeader("Create Note");

	// adds a single panel on second page (as there is only one panel, no menu will appear on the left side)
	dlg_create.addPanel("SinglePanel","<p><form id='create-note-form' action='../plugins/servlet/processservlet' method='get' id='h' class='aui unsectioned'><div style='padding-left:25px;'><table><tr><td>Priority<br/><input type='radio' name='priority' value='1' checked='yes' />Must<br/><input type='radio' name='priority' value='2'/>Could<br/></td><td rowspan='3'`><textarea class='no-resize' name='content' rows='10' cols='50'></textarea></td></tr><tr><td>Users Tagged<br/><select name='user'>#tagged_user()</select></td></tr><tr><td>Issues Tagged<br/><select name='issue'>#tagged_issue()</select></td></tr></table><input class='text' type='submit' name='create' value='Create'/><input class='text' type='reset' name='reset' value='Reset'/><input class='text' type='hidden' name='action' value='Create'/></div></form></p>", "singlePanel");
//	AJS.$("#create-note-link").attr('href', AJS.$("#create-note-link").attr('href') +'?_=1351099387275&decorator=dialog&inline=true');
	// Add events to dialog trigger elements
	AJS.$("#create-note-link").click(function() {
		AJS.$.ajax({
			url: AJS.$("#create-note-link").attr('href') + '?decorator=dialog&inline=true',
			type: "GET",
			error: function() { },
			success: function(data, textStatus, XMLHttpRequest) {
				AJS.$('#create-note-form').html(data);
			}
		});
		// PREPARE FOR DISPLAY
		// start first page, first panel
		dlg_create.gotoPage(0);
		dlg_create.gotoPanel(0);
		//update_chars_left(255, AJS.$('#create-note-form #update-content'), AJS.$('#create-note-form #update_remain'));
		dlg_create.show();
				
		return false;
	});
});